package lab.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class BoxPane extends JPanel {

	private static final long serialVersionUID = 1L;

	private String title = "";
	private Image northBackground = new ImageIcon("d:/images/box/top.png").getImage();
	private Image westBackground = new ImageIcon("d:/images/box/line.png").getImage();
	private Image eastBackground = new ImageIcon("d:/images/box/line.png").getImage();
	private Image southBackground = new ImageIcon("d:/images/box/buttom.png").getImage();
	private JPanel north = new ImagePanel(northBackground);
	private JPanel west = new ImagePanel(westBackground);
	private JPanel east = new ImagePanel(eastBackground);
	private JPanel south = new ImagePanel(southBackground);

	private JPanel contentPanel = new JPanel(new BorderLayout());
	private JLabel titleLabel = new JLabel();

	public BoxPane() {
		setOpaque(false);
		contentPanel.setOpaque(false);
		titleLabel.setOpaque(false);
		setLayout(new BorderLayout());
		north.setLayout(new BorderLayout());
		north.add(titleLabel);
		add(north, BorderLayout.NORTH);
		add(west, BorderLayout.WEST);
		add(east, BorderLayout.EAST);
		add(south, BorderLayout.SOUTH);
		add(contentPanel, BorderLayout.CENTER);
	}

	public void setTitle(String title, Icon icon, int align) {
		this.title = title;
		titleLabel.setText(title);
		titleLabel.setIcon(icon);
		titleLabel.setHorizontalAlignment(align);
	}

	public void setTitle(String title, int align) {
		setTitle(title, null, align);
	}

	public void setTitle(String title) {
		setTitle(title, null, JLabel.CENTER);
	}

	public void setTitleFont(Font font) {
		titleLabel.setFont(font);
	}

	public void setTitleColor(Color color) {
		titleLabel.setForeground(color);
	}

	public void setTitleSize(Dimension size) {
		north.setPreferredSize(size);
	}

	public void setTitleMargin(Insets margin) {
		JLabel label = new JLabel();
		label.setOpaque(false);
		label.setPreferredSize(new Dimension(0, margin.top));
		north.add(label, BorderLayout.NORTH);
		label = new JLabel();
		label.setOpaque(false);
		label.setPreferredSize(new Dimension(margin.left, 0));
		north.add(label, BorderLayout.WEST);
		label = new JLabel();
		label.setOpaque(false);
		label.setPreferredSize(new Dimension(0, margin.bottom));
		north.add(label, BorderLayout.SOUTH);
		label = new JLabel();
		label.setOpaque(false);
		label.setPreferredSize(new Dimension(margin.right, 0));
		north.add(label, BorderLayout.EAST);

	}

	public String getTitle() {
		return title;
	}

	public JPanel getContentPanel() {
		return contentPanel;
	}

	class ImagePanel extends JPanel {
		private static final long serialVersionUID = 1L;

		private Image background = null;

		public ImagePanel(Image background) {
			this.background = background;
			this.setOpaque(false);
			ImageIcon icon = new ImageIcon(background);
			this.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
		}

		@Override
		protected void paintComponent(Graphics g) {
			g.drawImage(background, 0, 0, this.getWidth(), this.getHeight(), this);
			super.paintComponent(g);
		}
	}
}
